const XLSX = require("xlsx");
const fs = require("fs");

module.exports = function (fileName,sellerId) {
  return new Promise((resolve, reject) => {
    if (Array.isArray(fileName)) {
      const workBook = XLSX.utils.book_new();
      const workSheet = XLSX.utils.json_to_sheet(fileName);
      XLSX.utils.book_append_sheet(workBook, workSheet, "Sheet1");
      XLSX.writeFile(workBook, `./excel/${sellerId}.xlsx`);
      resolve();
    } else {
      fs.readFile(fileName, "utf-8", (err, info) => {
        if (err) {
          console.log("文档不存在");
          reject();
        } else {
          let list = JSON.parse(info.replaceAll("][", ","));
          const workBook = XLSX.utils.book_new();
          const workSheet = XLSX.utils.json_to_sheet(list);
          XLSX.utils.book_append_sheet(workBook, workSheet, "Sheet1");
          XLSX.writeFile(workBook, `./excel/${sellerId}.xlsx`);
          resolve();
        }
      });
    }
  });
};
